import java.util.HashSet;
import java.util.Scanner;


public class function {
	
	private int n;
	private double fnorm;
	private Complex[] values;
    private Complex[] coeffs;
	private Integer[] charchters;
	private boolean fromXML;
	

	
	
	public function(int n)
	{
		this.n=n;
		this.values=new Complex[n];
		this.coeffs=new Complex[n];
		this.charchters=new Integer[n];
		
	}
	
	
	public void set_xml(boolean val)
	{
		this.fromXML=val;
	}

	
	
	public int get_n()
{
	return this.n;
	
}
	
	public double get_fnorm()
	{
		return fnorm;
	}
	
	
	
	public void set_fnorm(double d)
	{
		this.fnorm=d;
	}
	
	public function(int n, Complex[] coeffs, int[] alphas) {
	this.n = n;
	
	this.coeffs = new Complex[coeffs.length];
	for(int i = 0; i<this.coeffs.length; i++)
		this.coeffs[i] = new Complex (coeffs[i].getReal(),coeffs[i].getImaginary());
	
	this.charchters = new Integer[alphas.length];
	for(int i = 0; i<alphas.length; i++)
		this.charchters[i] = alphas[i];
	
	this.values = new Complex[n];
	}













public Complex apply(int i)
{
	if(this.fromXML){
	if(values[i]!=null)
		return values[(i+n)%n];
	else
	{
		values[i]=new Complex(0,0);
		for(int k = 0; k<coeffs.length; k++) {
		
			values[i].add(Complex.multiply(coeffs[k], Complex.Chi(this.charchters[k], i,n)));
		}

		
		return values[i];
	}
	}
	else
	{
	    Scanner readfunc=new Scanner(System.in);
		if(values[i]!=null)
			return values[i];
		System.out.println("Reading the"+i+"th value of the function f("+i+")");
		
		System.out.println("Please enter real part");
		double r=readfunc.nextDouble();
		System.out.println("Please enter the imaginary part" );
		double j=readfunc.nextDouble();
		Complex c=new Complex(r,j);
		values[i]=c;
		return c;
	}
	
}



public double getSquaredNorm()
{
	double magni;
	double sum=0;
	HashSet<Integer> omega=HelperClass.getRandomSet(Math.min(n, 100), n);
	
	
	for(Integer om:omega)
	{
	
	
	magni=apply(om).magnitude();
	 magni=Math.pow(magni, 2);
	 sum+=magni;
	}
	//System.out.println("returning:"+sum/omega.size());
	
	return sum/omega.size();
}










}
